<template>
	<view>
		<view class="beiwanglu-item" v-for="(item, i) in beiwangluList" :key="i">
			<view class="beiwanglu-info">
				<view>序号: {{ i + 1 }}</view>
				<view>标题: {{ item.title }}</view>
				<view>时间: {{ item.time }}</view>
			</view>
			<view class="btn-group">
				<button @click="removeBeiwanglu(item._id)">删除</button>
				<button @click="gotoEdit(item._id)">编辑</button>
			</view>
		</view>
		<button class="add-btn" @click="gotoAdd">添加备忘录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				beiwangluList: []
			};
		},
		onLoad() {
			this.getBeiwanglus();
		},
		methods: {
			getBeiwanglus() {
				console.log("--------------执行查询备忘录操作--------------");
				uniCloud.callFunction({
					name: "getBeiwanglu",
					success: (e) => {
						console.log(e.result);
						this.beiwangluList = e.result.data;
					}
				});
			},
			removeBeiwanglu(_id) {
				console.log("--------------执行删除备忘录操作--------------");
				uniCloud.callFunction({
					name: "removeBeiwanglu",
					data: {
						id: _id
					},
					success: (e) => {
						console.log(e.result);
						uni.showToast({
							title: "删除成功",
							duration: 2000
						});
						this.getBeiwanglus();
					}
				});
			},
			gotoEdit(_id) {
				console.log("--------------执行编辑备忘录跳转操作--------------");
				uni.navigateTo({
					url: "../Beiwanglu/edit?id=" + _id
				});
			},
			gotoAdd() {
				uni.navigateTo({
					url: "../Beiwanglu/add"
				});
			}
		}
	};
</script>

<style>
	.beiwanglu-item {
		border-bottom: 1rpx solid #F1F1F1;
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.beiwanglu-info {
		display: flex;
		flex-direction: column;
	}

	.btn-group {
		display: flex;
		gap: 10rpx;
	}

	.add-btn {
		margin: 20rpx;
		padding: 10rpx;
		background-color: #007AFF;
		color: #fff;
		border-radius: 5rpx;
	}
</style>